package com.xiyu.service.repository.medicine;

import com.xiyu.service.model.elder.info.ElderInfoFetcher;
import com.xiyu.service.model.medicine.HealthMedicineFetcher;
import com.xiyu.service.model.medicine.HealthMedicineRegisterFetcher;
import com.xiyu.service.vo.health.medicineRegister.MedicineRegisterPageQueryInput;
import com.xiyu.service.model.medicine.HealthMedicineRegister;
import com.xiyu.service.model.medicine.HealthMedicineRegisterTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;

import java.time.format.DateTimeFormatter;

public interface HealthMedicineRegisterRepository extends JRepository<HealthMedicineRegister, Long> {
    HealthMedicineRegisterTable healthMedicineRegisterTable = HealthMedicineRegisterTable.$;

    default Page<HealthMedicineRegister> pageQuery(MedicineRegisterPageQueryInput inputVO){
        return sql().createQuery(healthMedicineRegisterTable)
                .whereIf(inputVO.getUseTime() != null, () -> healthMedicineRegisterTable.useTime()
                        .between(inputVO.getUseTime()[0].format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
                                inputVO.getUseTime()[1].format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))))
                .orderBy(healthMedicineRegisterTable.id().desc())
                .select(healthMedicineRegisterTable.fetch(HealthMedicineRegisterFetcher.$.allTableFields()
                        .medicine(HealthMedicineFetcher.$.allTableFields())
                        .elder(ElderInfoFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }


}